package Offer.offer2017;

/**
 * 打印1到最大的N位数,主要在于考虑大数情况
 * Created by 雷文康 on 2017/12/30.
 */
public class Test12 {
    public static void main(String[] args) {
        print1ToN(3);
    }
    public static void print1ToN(int n) {
        if (n <= 0) {
            return;
        }
        int[] array = new int[n];
        //初始化数组
        for (int i = 0; i < n; i++) {
            array[i] = 0;
        }

        while (add(array)) {
            print(array);
        }
    }

    private static boolean add(int[] array) {
        int takeOver = 0;
        for (int i = array.length - 1; i >= 0; i--) {
            int sum = array[i] + takeOver;
            if (i == array.length - 1) {
                sum++;
            }
            if (sum == 10) {//表明有进位
                if (i == 0) {
                    return false;
                } else {
                    array[i] = 0;
                    takeOver = 1;
                }
            } else {
                array[i] = sum;
                break;
            }
        }
        return true;
    }

    private static void print(int[] array) {
        int i=0;
        for(i=0;i<array.length;i++){
            if(array[i]!=0){
                break;
            }
        }
        while(i<array.length){
            System.out.print(array[i++]);
        }
        System.out.print(" ");
    }
}
